<template>
  <div class="bg_img">
    <div class="form_sty">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model.number="ruleForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://127.0.0.1:7001";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            message: "长度至少6个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loginForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios
            .post("/role/rolelist", this.ruleForm)
            .then((res) => {
              console.log(res, "res");
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.message,
                  type: "success",
                });
                window.localStorage.setItem(
                  "token",
                  JSON.stringify(res.data.data.token)
                );
                window.localStorage.setItem(
                  "role",
                  JSON.stringify(res.data.data.userInfo)
                );
                alert(res.data.message);
                window.location.href = "/home";
              } else {
                this.$message({
                  message: res.data.message,
                  type: "warning",
                });
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register() {},
  },
};
</script>

<style lang="scss">
.bg_img {
  .form_sty {
    width: 30%;
    padding-top: 20%;
    margin: auto;
  }
}
</style>
